package com.xiaoyu.lanling.widget.picker.area;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.xiaoyu.lanling.R;

public class PickerAreaBottomSheetDialog {

    private Context mContext;

    private BottomSheetDialog mBottomSheetDialog;
    private View mView;
    private TextView mCancelButton;
    private TextView mOkButton;
    private PickerAreaView mPickerAreaView;

    private OnDateResultListener mOnDateResultListener;

    public static PickerAreaBottomSheetDialog create(Context context) {
        return new PickerAreaBottomSheetDialog(context);
    }

    private PickerAreaBottomSheetDialog(Context context) {
        this.mContext = context;
        initView();
    }

    private void initView() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.dialog_picker_area, new FrameLayout(mContext));
        mCancelButton = mView.findViewById(R.id.cancel);
        mOkButton = mView.findViewById(R.id.ok);
        mPickerAreaView = mView.findViewById(R.id.picker_area_view);

        mCancelButton.setOnClickListener(v -> dismiss());
        mOkButton.setOnClickListener(v -> {
            if (mOnDateResultListener != null) {
                mOnDateResultListener.onDateResult(mPickerAreaView.getProvinceValue(), mPickerAreaView.getCityValue(), mPickerAreaView.getCountyValue());
            }
            dismiss();
        });
    }

    public PickerAreaBottomSheetDialog scrollTo(String province, String city, String county) {
        if (mPickerAreaView != null) {
            mPickerAreaView.scrollTo(province, city, county);
        }
        return this;
    }

    public PickerAreaBottomSheetDialog show() {
        if (mBottomSheetDialog == null) {
            if (mContext != null && mView != null) {
                mBottomSheetDialog = new BottomSheetDialog(mContext);
                mBottomSheetDialog.setContentView(mView);
                mBottomSheetDialog.show();
            }
        } else {
            mBottomSheetDialog.show();
        }
        return this;
    }

    public PickerAreaBottomSheetDialog dismiss() {
        mBottomSheetDialog.dismiss();
        return this;
    }

    public PickerAreaBottomSheetDialog setOnDateResultListener(OnDateResultListener onDateResultListener) {
        this.mOnDateResultListener = onDateResultListener;
        return this;
    }

    public interface OnDateResultListener {
        void onDateResult(String province, String city, String county);
    }
}
